package com.example.demo.service.impl;

import com.example.demo.dto.RoutingFieldDTO;
import com.example.demo.entity.RoutingField;
import com.example.demo.exception.BusinessException;
import com.example.demo.mapper.RoutingFieldMapper;
import com.example.demo.request.CreateRoutingFieldRequest;
import com.example.demo.service.RoutingFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoutingFieldServiceImpl implements RoutingFieldService {

    @Autowired
    private RoutingFieldMapper routingFieldMapper;

    @Override
    public List<RoutingFieldDTO> getRoutingFields(int page, int size) {
        List<RoutingField> routingFields = routingFieldMapper.selectAll();
        return routingFields.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    @Override
    public long countRoutingFields() {
        return routingFieldMapper.count();
    }

    @Override
    public RoutingFieldDTO getRoutingFieldById(Integer id) {
        RoutingField routingField = routingFieldMapper.selectById(id);
        return convertToDTO(routingField);
    }

    @Override
    public RoutingField createRoutingField(CreateRoutingFieldRequest request) {
        // 检查路由字段英文名称是否已存在
        RoutingField existingField = routingFieldMapper.selectByEnglishName(request.getEnglishName());
        if (existingField != null) {
            throw new BusinessException("路由字段英文名称已存在：" + request.getEnglishName());
        }
        
        RoutingField routingField = new RoutingField();
        routingField.setEnglishName(request.getEnglishName());
        routingField.setChineseName(request.getChineseName());
        routingField.setDescription(request.getDescription());
        routingFieldMapper.insert(routingField);
        return routingField;
    }

    @Override
    public void updateRoutingField(Integer id, CreateRoutingFieldRequest request) {
        RoutingField routingField = routingFieldMapper.selectById(id);
        
        // 检查路由字段英文名称是否已存在（排除当前记录）
        if (!routingField.getEnglishName().equals(request.getEnglishName())) {
            RoutingField existingField = routingFieldMapper.selectByEnglishNameExcludeId(request.getEnglishName(), id);
            if (existingField != null) {
                throw new BusinessException("路由字段英文名称已存在：" + request.getEnglishName());
            }
        }
        
        routingField.setEnglishName(request.getEnglishName());
        routingField.setChineseName(request.getChineseName());
        routingField.setDescription(request.getDescription());
        routingFieldMapper.updateById(routingField);
    }

    @Override
    public void deleteRoutingField(Integer id) {
        routingFieldMapper.deleteById(id);
    }
    
    @Override
    public List<RoutingFieldDTO> searchRoutingFieldsByChineseName(String chineseName) {
        List<RoutingField> routingFields = routingFieldMapper.selectByChineseNameLike(chineseName);
        return routingFields.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    private RoutingFieldDTO convertToDTO(RoutingField routingField) {
        return RoutingFieldDTO.fromEntity(routingField);
    }
}